package com.ruoyi.web.controller.company.datafile;

import java.io.IOException;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.company.datafile.domain.ComDataFile;
import com.ruoyi.company.datafile.service.IComDataFileService;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据文件信息Controller
 * 
 * @author baike
 * @date 2020-12-19
 */
@Controller
@RequestMapping("/company/datafile")
public class ComDataFileController extends BaseController
{
    private String prefix = "company/datafile";

    @Autowired
    private IComDataFileService comDataFileService;

    @RequiresPermissions("company:datafile:view")
    @GetMapping()
    public String datafile()
    {
        return prefix + "/datafile";
    }

    /**
     * 查询数据文件信息列表
     */
    @RequiresPermissions("company:datafile:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ComDataFile comDataFile)
    {
        startPage();
        List<ComDataFile> list = comDataFileService.selectComDataFileList(comDataFile);
        return getDataTable(list);
    }

    /**
     * 导出数据文件信息列表
     */
    @RequiresPermissions("company:datafile:export")
    @Log(title = "数据文件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ComDataFile comDataFile)
    {
        List<ComDataFile> list = comDataFileService.selectComDataFileList(comDataFile);
        ExcelUtil<ComDataFile> util = new ExcelUtil<ComDataFile>(ComDataFile.class);
        return util.exportExcel(list, "datafile");
    }

    /**
     * 新增数据文件信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据文件信息
     * @throws IOException 
     */
    @RequiresPermissions("company:datafile:add")
    @Log(title = "数据文件信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file,@RequestParam(value="fileStatus") String fileStatus,@RequestParam(value="invalidTime") String invalidTime,ComDataFile comDataFile) throws IOException
    {
    	//获取源文件名称
    	String originalFilename = StringUtils.substringBefore(file.getOriginalFilename(), ".");
    	//获取文件类型
    	String fileType = StringUtils.substringAfter(file.getOriginalFilename(), ".");
    	//获取当前用户
    	String createUser = ShiroUtils.getLoginName();
    	//上传文件路径
    	String filePath = RuoYiConfig.getUploadPath();
    	// 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        comDataFile.setFilePath(fileName);
        comDataFile.setFileStatus(fileStatus);
        comDataFile.setInvalidTime(DateUtils.parseDate(invalidTime));
        comDataFile.setFileType(fileType);
        comDataFile.setFileName(originalFilename);
        comDataFile.setCreateUser(createUser);
        
        
        return toAjax(comDataFileService.insertComDataFile(comDataFile));
    }

    /**
     * 修改数据文件信息
     */
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") Long fileId, ModelMap mmap)
    {
        ComDataFile comDataFile = comDataFileService.selectComDataFileById(fileId);
        mmap.put("comDataFile", comDataFile);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据文件信息
     */
    @RequiresPermissions("company:datafile:edit")
    @Log(title = "数据文件信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ComDataFile comDataFile)
    {
        return toAjax(comDataFileService.updateComDataFile(comDataFile));
    }

    /**
     * 删除数据文件信息
     */
    @RequiresPermissions("company:datafile:remove")
    @Log(title = "数据文件信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(comDataFileService.deleteComDataFileByIds(ids));
    }
}
